package com.kk.base.controller.admin;

import com.kk.base.bean.CodeMsg;
import com.kk.base.bean.PageBean;
import com.kk.base.bean.Result;
import com.kk.base.entity.admin.ProcessRecords;
import com.kk.base.entity.admin.User;
import com.kk.base.entity.common.ExpressMailOrderStatus;
import com.kk.base.service.admin.ProcessRecordsService;
import com.kk.base.service.admin.UserService;
import com.kk.base.util.SessionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/admin/processRecords")
public class ProcessRecordsController {

    @Autowired
    private ProcessRecordsService processRecordsService;


    @Autowired
    private UserService userService;

    /**
     * 进入运输收货列表根据订单编号查询
     * @param pageBean
     * @param waybillNumber
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(PageBean<ProcessRecords> pageBean, String waybillNumber, Model model) {
        User loginedUser = SessionUtil.getLoginedUser();
        model.addAttribute("title", "运输收货列表");
        model.addAttribute("waybillNumber", waybillNumber);
        model.addAttribute("UserRoleTypeEnum",loginedUser.getUserType().getCode());
        model.addAttribute("pageBean", processRecordsService.findList(pageBean, waybillNumber));
        return "admin/process_records/list";
    }

    /**
     * 进入执行运输列表根据订单编号查询
     * @param pageBean
     * @param waybillNumber
     * @param model
     * @return
     */
    @RequestMapping("/receiveList")
    public String receiveList(PageBean<ProcessRecords> pageBean, String waybillNumber, Model model) {
        model.addAttribute("title", "运输列表");
        model.addAttribute("waybillNumber", waybillNumber);
        model.addAttribute("users",userService.findByUserTypeNot());
        model.addAttribute("pageBean", processRecordsService.findListByWaybillNumber(pageBean, waybillNumber));
        return "admin/process_records/receive_list";
    }

    /**
     * 网点拾取快递并添加流程记录
     * @param id
     * @return
     */
    @RequestMapping("/takeDeliveryOfGoods")
    @ResponseBody
    public Result<Boolean> takeDeliveryOfGoods(@RequestParam(name="id",required=true)Long id){

        if (null == id){
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER1_RECEIVING_EDIT_ERROR);
        }

        ProcessRecords processRecords = processRecordsService.find(id);
        processRecords.setOperation(ProcessRecords.HAVE_HANDLED);

        //讲提交的管理员信息指定字段复制到已存在的user对象中,该方法会覆盖新字段内容
        ProcessRecords newProcessRecords = new ProcessRecords();
        BeanUtils.copyProperties(processRecords, newProcessRecords,"id","createTime","updateTime");
        newProcessRecords.setTravelPosition(ExpressMailOrderStatus.INTRANSIT);
        newProcessRecords.setId(null);
        newProcessRecords.setOperation(ProcessRecords.NO_OPERATIONS);
        ProcessRecords saveNew = processRecordsService.save(newProcessRecords);
        if (null == saveNew){
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER1_RECEIVING_EDIT_ERROR);
        }
        ProcessRecords save = processRecordsService.save(processRecords);
        if (null == save){
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER1_RECEIVING_EDIT_ERROR);
        }
        return Result.success(true);
    }

    /**
     * 设置快递继续运输完成
     * @param id
     * @param userId
     * @return
     */
    @RequestMapping("/transportation")
    @ResponseBody
    public Result<Boolean> transportation(@RequestParam(name="id",required=true)Long id,
                                          @RequestParam(name="userId",required=true)Long userId){

        if (null == id){
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER_SENDTHECARGO_ADD_EDIT_ERROR);
        }

        if (null == userId){
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER_USER_NULL);
        }

        Integer integer = processRecordsService.transportation(id,userId);

        if(integer == 1){
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER_SENDTHECARGO_ADD_EDIT_ERROR);
        }
        return Result.success(true);
    }

    /**
     * 指定快递员
     * @param id
     * @return
     */
    @RequestMapping("/delivery")
    @ResponseBody
    public Result<Boolean> delivery(@RequestParam(name="id",required=true)Long id){

        if (null == id){
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER_SENDTHECARGO_ADD_EDIT_ERROR);
        }

        Integer integer = processRecordsService.delivery(id);

        if(integer == 1){
            return Result.error(CodeMsg.ADMIN_EXPRESSMAILORDER_SENDTHECARGO_ADD_EDIT_ERROR);
        }
        return Result.success(true);
    }




}
